//
//  NHT_SelectDateView.swift
//  BalticSwift
//
//  Created by NHT on 2023/7/4.
//

import UIKit

/**
 使用方法
 var dateView = NHT_SelectDateView.init();
 dateView.frame = CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: 400);
 self.view.addSubview(dateView);
 
 dateView.max_Time_Str = "2023-11-11 12:12:12";//可配置选择最大日期
 dateView.min_Time_Str = "2011-11-11 11:11:11";//可配置选择最小日期
 dateView.textFont_Selected = UIFont.systemFont(ofSize: 20);//选中文字字体
 dateView.textColor_Selected = UIColor.blue;//选中文字颜色
 dateView.createDatePick(dateMode: .YearMonthDayHourMinuteSecond)//创建选择器
 dateView.resetMidView = {midView in
     //配置中间选中view 可不实现
     midView.backgroundColor = UIColor.yellow.withAlphaComponent(0.3)
 };
 dateView.selectTimeBlock = {timeStr in
     //选择之后的回调
     //print("timeStr:",timeStr)
 }
 */

/**
 注意
 1、 datePickerMine.selectRow 执行后不会调用didSelectRow，需要手动设置值
 2、 nowSelectMode
 根据不同的内容 设置对应的列数 numberOfComponents
 比如只选择年就一列，选择年月日就三列
 3、刷新数据
 每次更新了年的数据，就自动判断现在是否显示月的数据，如果显示就自动更新月的数据，但是第一次添加数据时不联动，天、小时等同理
 4、怎么判断当前数据需要在限制的时间内
 选择当前年就是限制的年（最小、最大），那就需要判断
 选择的当前年、当前月是限制的年和月（最小、最大），那就需要判断
 */

public class NHT_SelectDateView: UIView,UIPickerViewDataSource,UIPickerViewDelegate {
    
    //MARK: 自定义中间选中的view
    /// 中间选中的view，可以对其进行自定义
    private var midSelectView:UIView? = nil;
    /// 可通过这个回调自定义中间 选中的view
    public var resetMidView: ((UIView) ->Void)?
    public override func didMoveToWindow() {
        if datePickerMine.subviews.count > 1{
            midSelectView = datePickerMine.subviews[1];
            resetMidView?(midSelectView!);
        }
    }


    /// 默认年最大值
    static let MAXYEAR = 2999;
    /// 默认年最小值
    static let MINYEAR = 1900;


   private lazy var datePickerMine: UIPickerView = {
        let datePicker = UIPickerView.init();
        datePicker.backgroundColor = UIColor.white;
        datePicker.delegate = self;
        datePicker.dataSource = self;
        self.addSubview(datePicker);
        datePicker.frame = self.bounds;
        return datePicker
    }()
    //MARK: 必须调用这个方法才可以创建
    public func createDatePick(dateMode:NHTDateMode? = .YearMonthDayHourMinuteSecond) -> Void {
        self.datePickerMine.isHidden = false;
        dateSource.removeAll();
        nowSelectMode = dateMode!;
    }
    ///选择一个日期之后的回调
    public var selectTimeBlock:((String) ->Void)?

    /// 返回当前选中的实际那
    private func returnNowSelectTime() -> Void {

        var timeStr = "";
        switch nowSelectMode {
        case .Year:
            timeStr = getThisInfo(component: component_Year, row: row_Year);
            break
        case .YearMonth:
            timeStr = getThisInfo(component: component_Year, row: row_Year) + "-" + getThisInfo(component: component_Month, row: row_Month);
            break
        case .YearMonthDay:
            timeStr = getThisInfo(component: component_Year, row: row_Year) + "-" + getThisInfo(component: component_Month, row: row_Month) + "-" + getThisInfo(component: component_Day, row: row_Day);
            break
        case .DayMonthYear:
            timeStr = getThisInfo(component: component_Day, row: row_Day) + "-" + getThisInfo(component: component_Month, row: row_Month) + "-" + getThisInfo(component: component_Year, row: row_Year);
            break
        case .YearMonthDayHour:
            timeStr = getThisInfo(component: component_Year, row: row_Year) + "-" + getThisInfo(component: component_Month, row: row_Month) + "-" + getThisInfo(component: component_Day, row: row_Day) + " " + getThisInfo(component: component_Hour, row: row_Hour);
            break
        case .YearMonthDayHourMinute:
            timeStr = getThisInfo(component: component_Year, row: row_Year) + "-" + getThisInfo(component: component_Month, row: row_Month) + "-" + getThisInfo(component: component_Day, row: row_Day) + " " + getThisInfo(component: component_Hour, row: row_Hour) + ":" + getThisInfo(component: component_Minute, row: row_Minute);
            break
        case .YearMonthDayHourMinuteSecond:
            timeStr = getThisInfo(component: component_Year, row: row_Year) + "-" + getThisInfo(component: component_Month, row: row_Month) + "-" + getThisInfo(component: component_Day, row: row_Day) + " " + getThisInfo(component: component_Hour, row: row_Hour) + ":" + getThisInfo(component: component_Minute, row: row_Minute) + ":" + getThisInfo(component: component_Second, row: row_Second);
            break
        }
        selectTimeBlock?(timeStr);
    }
    func getThisInfo(component:Int,row:Int) -> String {
        var thisInfo = dateSource[component][row];
        if thisInfo.count < 2{
            thisInfo = "0" + thisInfo;
        }
        return thisInfo;
    }

    //MARK: 选择代理方法
    public func numberOfComponents(in pickerView: UIPickerView) -> Int {
        //总共几列
        return dateSource.count;
    }
    public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        //总共几行
        return dateSource[component].count;
    }
    public func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        //一行的高度
        return 44;
    }
    public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        //每一行显示内容
        return self.dateSource[component][row];
    }
    public func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let labelShow = (view as? UILabel) ?? UILabel()
        labelShow.font = textFont_Normal;
        labelShow.textColor = textColor_Normal;
        if (component == component_Year && row == row_Year) || (component == component_Month && row == row_Month) || (component == component_Day && row == row_Day) || (component == component_Hour && row == row_Hour) || (component == component_Minute && row == row_Minute) || (component == component_Second && row == row_Second){
            if textFont_Selected != nil{
                labelShow.font = textFont_Selected;
            }
            if textColor_Selected != nil{
                labelShow.textColor = textColor_Selected;
            }
        }

//        labelShow.backgroundColor = UIColor.blue;
//        let paraStyle = NSMutableParagraphStyle()
//        paraStyle.firstLineHeadIndent = 9.0 //首行缩进
//        let attrText = NSAttributedString(string: self.dateSource[component][row], attributes: [.paragraphStyle: paraStyle])
//        labelShow.attributedText = attrText;
        labelShow.text = self.dateSource[component][row];
        labelShow.textAlignment = .center;
        return labelShow
    }


    public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        //print("didSelectRow: ",row,"component:",component);
        if component == component_Year {
            row_Year = row;
        }
        if component == component_Month {
            row_Month = row;
        }
        if component == component_Day {
            row_Day = row;
        }
        if component == component_Hour {
            row_Hour = row;
        }
        if component == component_Minute {
            row_Minute = row;
        }
        if component == component_Second {
            row_Second = row;
        }
        pickerView.reloadComponent(component);
        self.returnNowSelectTime()
    }

    //MARK: 设置选择格式
    ///当期选择时间的类型，默认日月年
    var nowSelectMode: NHTDateMode = .YearMonthDay{
        didSet{
            self.setNowDateMode();
            self.setComponent();
            self.setCanShow();
            self.setDefaultDataSource();
        }
    }
    ///当前时间格式 yyyy-MM-dd HH:mm:ss
    var nowDateModeStr:String = "" {
        didSet{
            timeStrToDate(timeStr: min_Time_Str, isMin:true);
            timeStrToDate(timeStr: max_Time_Str, isMin:false);
            setDefaultSelectTime();
        }
    };

    //MARK: 当前选择内容对应row（行）
    ///年当前显示第几行
    private var row_Year: Int = 0{
        didSet{
            //更新月信息
            self.refreshInfo_Month()
        }
    };
    ///月当前显示第几行
    private var row_Month: Int = 0{
        didSet{
            //更新天信息
            self.refreshInfo_Day()
        }
    };
    ///天当前显示第几行
    private var row_Day: Int = 0{
        didSet{
            //更新小时信息
            self.refreshInfo_Hour();
        }
    };
    ///小时当前显示第几行
    private var row_Hour: Int = 0{
        didSet{
            //更新分钟信息
            self.refreshInfo_Minute();
        }
    };
    ///分钟当前显示第几行
    private var row_Minute: Int = 0{
        didSet{
            //更新秒信息
            self.refreshInfo_Second();
        }
    };
    ///秒当前显示第几行
    private var row_Second = 0;




    //MARK: 数据获取
    ///总数据
    private var dateSource:[[String]] = [];
    ///配置默认显示内容
    private func setDefaultDataSource() -> Void {
        /**按照顺序配置年月日时分秒信息*/
        self.refreshInfo_Year(false)
        self.refreshInfo_Month(false)
        self.refreshInfo_Day(false)
        self.refreshInfo_Hour(false)
        self.refreshInfo_Minute(false)
        self.refreshInfo_Second(false)
        setDefaultSelectTime();
    }
    //MARK: 更新信息
    ///更新年的信息
    private func refreshInfo_Year(_ linkage:Bool? = true) -> Void {
        if !canShow_Year {
            return
        }
        //print("refreshInfo_Year年");
        let infoArr = getWillShowInfo(min: min_Year, max: max_Year);
        dateSource.replaceSubrange(component_Year...component_Year, with: [infoArr])
        self.datePickerMine.reloadComponent(component_Year)
        if row_Year >= infoArr.count {
            self.datePickerMine.selectRow(infoArr.count-1, inComponent: component_Year, animated: true)
            row_Year = infoArr.count-1;
        }
        if linkage! && canShow_Month{
            self.refreshInfo_Month()
        }
    }
    ///更新月的信息
    private func refreshInfo_Month(_ linkage:Bool? = true) -> Void {
        if !canShow_Month {
            return
        }
        //print("refreshInfo_Month月");
        //判断当前可以显示的最大最小值
        let minMaxValueArr = checkCanShowMinMaxValue(type: 1);

        let infoArr = getWillShowInfo(min: minMaxValueArr[0], max: minMaxValueArr[1]);
        dateSource.replaceSubrange(component_Month...component_Month, with: [infoArr])
        self.datePickerMine.reloadComponent(component_Month)
        if row_Month >= infoArr.count {
            self.datePickerMine.selectRow(infoArr.count-1, inComponent: component_Month, animated: true)
            row_Month = infoArr.count-1;
        }
        if linkage! && canShow_Day{
            self.refreshInfo_Day()
        }
    }

    ///更新天的信息
    private func refreshInfo_Day(_ linkage:Bool? = true) -> Void {
        if !canShow_Day {
            return
        }
        //print("refreshInfo_Day日");
        let yearInt = Int(self.dateSource[component_Year][row_Year])!;
        let month = self.dateSource[component_Month][row_Month];
        var dayCount = 30;//这个月多少天
        //31天的月份
        let month_31 = ["1","3","5","7","8","12"];
        if month == "2"{
            //是不是闰年
            dayCount = yearInt % 4 == 0 ? 29:28;
        }else if month_31.contains(month){
            dayCount = 31;
        }

        //判断当前可以显示的最大最小值
        let minMaxValueArr = checkCanShowMinMaxValue(type: 2);
        dayCount = dayCount > minMaxValueArr[1] ? minMaxValueArr[1]:dayCount;

        let infoArr = getWillShowInfo(min: minMaxValueArr[0], max: dayCount);
        dateSource.replaceSubrange(component_Day...component_Day, with: [infoArr])
        self.datePickerMine.reloadComponent(component_Day)

        if row_Day >= infoArr.count {
            self.datePickerMine.selectRow(infoArr.count-1, inComponent: component_Day, animated: true)
            row_Day = infoArr.count - 1;
        }
        if linkage! && canShow_Hour{
            refreshInfo_Hour();
        }
    }
    ///更新小时的信息
    private func refreshInfo_Hour(_ linkage:Bool? = true) -> Void {
        if !canShow_Hour {
            return
        }
        //print("refreshInfo__Hour小时");
        //判断当前可以显示的最大最小值
        let minMaxValueArr = checkCanShowMinMaxValue(type: 3);

        let infoArr = getWillShowInfo(min: minMaxValueArr[0], max: minMaxValueArr[1]);
        dateSource.replaceSubrange(component_Hour...component_Hour, with: [infoArr])
        self.datePickerMine.reloadComponent(component_Hour)
        if row_Hour >= infoArr.count {
            self.datePickerMine.selectRow(infoArr.count-1, inComponent: component_Hour, animated: true)
            row_Hour = infoArr.count-1;
        }
        if linkage! && canShow_Minute{
            self.refreshInfo_Minute()
        }
    }
    ///更新分钟的信息
    private func refreshInfo_Minute(_ linkage:Bool? = true) -> Void {
        if !canShow_Minute {
            return
        }
        //print("refreshInfo___Minute分钟");
        //判断当前可以显示的最大最小值
        let minMaxValueArr = checkCanShowMinMaxValue(type: 4);

        let infoArr = getWillShowInfo(min: minMaxValueArr[0], max: minMaxValueArr[1]);
        dateSource.replaceSubrange(component_Minute...component_Minute, with: [infoArr])
        self.datePickerMine.reloadComponent(component_Minute)
        if row_Minute >= infoArr.count {
            self.datePickerMine.selectRow(infoArr.count-1, inComponent: component_Minute, animated: true)
            row_Minute = infoArr.count-1;
        }
        if linkage! && canShow_Second{
            self.refreshInfo_Second()
        }
    }
    ///更新秒的信息
    private func refreshInfo_Second(_ linkage:Bool? = true) -> Void {
        if !canShow_Second {
            return
        }
        //print("refreshInfo___Second秒");
        //判断当前可以显示的最大最小值
        let minMaxValueArr = checkCanShowMinMaxValue(type: 5);

        let infoArr = getWillShowInfo(min: minMaxValueArr[0], max: minMaxValueArr[1]);
        dateSource.replaceSubrange(component_Second...component_Second , with: [infoArr])
        self.datePickerMine.reloadComponent(component_Second )
        if row_Second  >= infoArr.count {
            self.datePickerMine.selectRow(infoArr.count-1, inComponent: component_Second , animated: true)
            row_Second  = infoArr.count-1;
        }
    }
    //MARK: 公用方法
    ///获取可显示数组
    private func getWillShowInfo(min:Int,max:Int) -> [String] {
        var showArr:[String] = [];
        for index in min...max{
            showArr.append(String(index));
        }
        return showArr;
    }
    /// 检查获取当前可显示的最大最小值
    /// - Parameter type: 0:年；1：月；2：日；3：时，4：分；5：秒；
    private func checkCanShowMinMaxValue(type:Int) -> [Int] {
        var needCheck = type;
        var isMin = false;
        var isMax = false;
        let haveMinTime = min_Time != nil ? true:false;
        let haveMaxTime = max_Time != nil ? true:false;
        var minValue = 1;
        var maxValue = 12;
        if needCheck > 0{
            needCheck -= 1;
            if haveMinTime && row_Year == 0{
                isMin = true;
                minValue = min_Month;
            }

            if haveMaxTime && row_Year == self.dateSource[component_Year].count - 1{
                isMax = true;
                maxValue = max_Month;
            }
        }


        if needCheck > 0{
            needCheck -= 1;
            //天
            minValue = 1;
            if haveMinTime && isMin && row_Month == 0{
                isMin = true;
                minValue = min_Day;
            }else{
                isMin = false;
            }

            maxValue = 31;
            if haveMaxTime && isMax && row_Month == self.dateSource[component_Month].count - 1{
                isMax = true;
                maxValue = max_Day;
            }else{
                isMax = false;
            }
        }

        if needCheck > 0{
            needCheck -= 1;
            //时
            minValue = 0;
            if haveMinTime && isMin && row_Day == 0{
                isMin = true;
                minValue = min_Hour;
            }else{
                isMin = false;
            }

            maxValue = 23;
            if haveMaxTime && isMax && row_Day == self.dateSource[component_Day].count - 1{
                isMax = true;
                maxValue = max_Hour;
            }else{
                isMax = false;
            }
        }
        if needCheck > 0{
            needCheck -= 1;
            //分
            minValue = 0;
            if haveMinTime && isMin && row_Hour == 0{
                isMin = true;
                minValue = min_Minute;
            }else{
                isMin = false;
            }

            maxValue = 59;
            if haveMaxTime && isMax && row_Hour == self.dateSource[component_Hour].count - 1{
                isMax = true;
                maxValue = max_Minute;
            }else{
                isMax = false;
            }
        }
        if needCheck > 0{
            needCheck -= 1;
            //秒
            minValue = 0;
            if haveMinTime && isMin && row_Minute == 0{
                isMin = true;
                minValue = min_Second;
            }else{
                isMin = false;
            }

            maxValue = 59;
            if haveMaxTime && isMax && row_Minute == self.dateSource[component_Minute].count - 1{
                isMax = true;
                maxValue = max_Second;
            }else{
                isMax = false;
            }
        }

        return [minValue,maxValue];
    }

    //MARK: 对应component（列），只有设置日期格式时会改变
    ///年显示第几列
    private var component_Year = 0;
    ///月显示第几列
    private var component_Month = 1;
    ///天显示第几列
    private  var component_Day = 2;
    ///小时显示第几列
    private var component_Hour = 3;
    ///分钟显示第几列
    private var component_Minute = 4;
    ///秒显示第几列
    private var component_Second = 5;
    ///配置年月日时分秒 各自的component
    private func setComponent() -> Void {
        switch nowSelectMode {
        case .Year:
            component_Year = 0;
            break
        case .YearMonth:
            component_Year = 0;
            component_Month = 1;
            break
        case .YearMonthDay:
            component_Year = 0;
            component_Month = 1;
            component_Day = 2;
            break
        case .DayMonthYear:
            component_Year = 2;
            component_Month = 1;
            component_Day = 0;
            break
        case .YearMonthDayHour:
            component_Year = 0;
            component_Month = 1;
            component_Day = 2;
            component_Hour = 3;
            break
        case .YearMonthDayHourMinute:
            component_Year = 0;
            component_Month = 1;
            component_Day = 2;
            component_Hour = 3;
            component_Minute = 4;
            break
        case .YearMonthDayHourMinuteSecond:
            component_Year = 0;
            component_Month = 1;
            component_Day = 2;
            component_Hour = 3;
            component_Minute = 4;
            component_Second = 5;
            break
        }

    }
    //MARK: 当前可以显示的内容，只有设置日期格式时会改变
    ///年  是否可显示
    private var  canShow_Year:Bool = false{
        willSet{
            if newValue{
                self.dateSource.append([""]);
            }
        }
    };
    ///月  是否可显示
    private var  canShow_Month:Bool = false{
        willSet{
            if newValue{
                self.dateSource.append([""]);
            }
        }
    };
    ///天  是否可显示
    private var  canShow_Day:Bool = false{
        willSet{
            if newValue{
                self.dateSource.append([""]);
            }
        }
    };
    ///小时  是否可显示
    private var  canShow_Hour:Bool = false{
        willSet{
            if newValue{
                self.dateSource.append([""]);
            }
        }
    };
    ///分钟  是否可显示
    private var  canShow_Minute:Bool = false{
        willSet{
            if newValue{
                self.dateSource.append([""]);
            }
        }
    };
    ///秒  是否可显示
    private var  canShow_Second:Bool = false{
        willSet{
            if newValue{
                self.dateSource.append([""]);
            }
        }
    };
    ///配置年月日时分秒 是否可以显示选择
    private func setCanShow() -> Void {
        switch nowSelectMode {
        case .Year:
            canShow_Year = true;
            break
        case .YearMonth:
            canShow_Year = true;
            canShow_Month = true;
            break
        case .YearMonthDay:
            canShow_Year = true;
            canShow_Month = true;
            canShow_Day = true;
            break
        case .DayMonthYear:
            canShow_Year = true;
            canShow_Month = true;
            canShow_Day = true;
            break
        case .YearMonthDayHour:
            canShow_Year = true;
            canShow_Month = true;
            canShow_Day = true;
            canShow_Hour = true;
            break
        case .YearMonthDayHourMinute:
            canShow_Year = true;
            canShow_Month = true;
            canShow_Day = true;
            canShow_Hour = true;
            canShow_Minute = true;
            break
        case .YearMonthDayHourMinuteSecond:
            canShow_Year = true;
            canShow_Month = true;
            canShow_Day = true;
            canShow_Hour = true;
            canShow_Minute = true;
            canShow_Second = true;
            break
        }
    }
    ///配置日期格式
    func setNowDateMode() -> Void {
        switch nowSelectMode {
        case .Year:
            nowDateModeStr = "yyyy";
            break
        case .YearMonth:
            nowDateModeStr = "yyyy-MM";
            break
        case .YearMonthDay:
            nowDateModeStr = "yyyy-MM-dd";
            break
        case .DayMonthYear:
            nowDateModeStr = "dd-MM-yyyy";
            break
        case .YearMonthDayHour:
            nowDateModeStr = "yyyy-MM-dd HH";
            break
        case .YearMonthDayHourMinute:
            nowDateModeStr = "yyyy-MM-dd HH:mm";
            break
        case .YearMonthDayHourMinuteSecond:
            nowDateModeStr = "yyyy-MM-dd HH:mm:ss";
            break
        }
    }

    //MARK: 最大值
    public var max_Time_Str:String = ""{
        didSet{
            timeStrToDate(timeStr: max_Time_Str, isMin:false);
        }
    }
    ///时间可选择最大值，默认不限制为nil
    private var max_Time:Date? = nil{
        didSet{
            //配置新的最大值
            let calendar = NSCalendar.autoupdatingCurrent;
            let maxComponents = calendar.dateComponents([.year,.month,.day,.weekOfMonth,.hour,.minute,.second,.weekday,.weekdayOrdinal], from: max_Time! as Date);
            max_Year = maxComponents.year ?? NHT_SelectDateView.MAXYEAR;
            max_Month = maxComponents.month ?? 12;
            max_Day = maxComponents.day ?? 31;
            max_Hour = maxComponents.hour ?? 23;
            max_Minute = maxComponents.minute ?? 59;
            max_Second = maxComponents.second ?? 59;
        }
    }
    ///年最大值
    private var  max_Year = MAXYEAR;
    ///月最大值
    private var  max_Month = 12;
    ///天最大值
    private var  max_Day = 31;
    ///小时最大值
    private var  max_Hour = 23;
    ///分钟最大值
    private var  max_Minute = 59;
    ///秒最大值
    private var  max_Second = 59;



    //MARK: 最小值
    public var min_Time_Str:String = ""{
        didSet{
            timeStrToDate(timeStr: min_Time_Str, isMin:true);
        }
    }
    ///时间可选择最小值，默认不限制为nil
    private var min_Time:Date? = nil{
        didSet{
            //配置新的最小值
            let  calendar = NSCalendar.autoupdatingCurrent;
            let minComponents = calendar.dateComponents([.year,.month,.day,.weekOfMonth,.hour,.minute,.second,.weekday,.weekdayOrdinal], from:min_Time! as Date);
            min_Year = minComponents.year ?? NHT_SelectDateView.MINYEAR;
            min_Month = minComponents.month ?? 1;
            min_Day = minComponents.day ?? 1;
            min_Hour = minComponents.hour ?? 0;
            min_Minute = minComponents.minute ?? 0;
            min_Second = minComponents.second ?? 0;
        }
    }
    ///年最小值
    private var  min_Year = MINYEAR;
    ///月最小值
    private var  min_Month = 1;
    ///天最小值
    private var  min_Day = 1;
    ///小时最小值
    private var  min_Hour = 0;
    ///分钟最小值
    private var  min_Minute = 0;
    ///秒最小值
    private var  min_Second = 0;

    func timeStrToDate(timeStr:String,isMin:Bool) -> Void {
        if nowDateModeStr.count > 0{
            let formatter = DateFormatter.init()
            formatter.dateFormat = nowDateModeStr;
            let timeDate = formatter.date(from: timeStr);
            if timeDate != nil{
                if isMin{
                    min_Time = timeDate;
                }else{
                    max_Time = timeDate;
                }
                setDefaultDataSource()
            }
        }
    }

    //MARK: 配置当前选中的时间
    /// 当前选中的时间
    var defaultSelectTime:String = "" {
        didSet{
            setDefaultSelectTime();
        }
    }
    ///滚动到当前选中时间
    private func setDefaultSelectTime() -> Void {
        if nowDateModeStr.count > 0 && defaultSelectTime.count >= nowDateModeStr.count{
            //将设置的默认时间字符长度和时间格式长多剪切为一致的，不一致设置不生效
            var newDefaultTime = defaultSelectTime;
            while newDefaultTime.count > nowDateModeStr.count {
                newDefaultTime.removeLast()
            }
            let formatter = DateFormatter.init()
            formatter.dateFormat = nowDateModeStr;
            let timeDate = formatter.date(from: newDefaultTime);
            if timeDate != nil{
                let calendar = NSCalendar.autoupdatingCurrent;
                let nowomponents = calendar.dateComponents([.year,.month,.day,.weekOfMonth,.hour,.minute,.second,.weekday,.weekdayOrdinal], from:timeDate! as Date);

                if canShow_Year{
                    let now_componentSource = dateSource[component_Year];
                    let index = now_componentSource.firstIndex(of: String(nowomponents.year!)) ?? -1;
                    if index != -1 && now_componentSource.count > index{
                        row_Year = index;
                        datePickerMine.selectRow(row_Year, inComponent: component_Year, animated: true);
                    }
                }
                if canShow_Month{
                    let now_componentSource = dateSource[component_Month];
                    let index = now_componentSource.firstIndex(of: String(nowomponents.month!)) ?? -1;
                    if index != -1 && now_componentSource.count > index{
                        row_Month = index;
                        datePickerMine.selectRow(row_Month, inComponent: component_Month, animated: true);
                    }
                }
                if canShow_Day{
                    let now_componentSource = dateSource[component_Day];
                    let index = now_componentSource.firstIndex(of: String(nowomponents.day!)) ?? -1;
                    if index != -1 && now_componentSource.count > index{
                        row_Day = index;
                        datePickerMine.selectRow(row_Day, inComponent: component_Day, animated: true);
                    }
                }
                if canShow_Hour{
                    let now_componentSource = dateSource[component_Hour];
                    let index = now_componentSource.firstIndex(of: String(nowomponents.hour!)) ?? -1;
                    if index != -1 && now_componentSource.count > index{
                        row_Hour = index;
                        datePickerMine.selectRow(row_Hour, inComponent: component_Hour, animated: true);
                    }
                }
                if canShow_Minute{
                    let now_componentSource = dateSource[component_Minute];
                    let index = now_componentSource.firstIndex(of: String(nowomponents.minute!)) ?? -1;
                    if index != -1 && now_componentSource.count > index{
                        row_Minute = index;
                        datePickerMine.selectRow(row_Minute, inComponent: component_Minute, animated: true);
                    }
                }
                if canShow_Second{
                    let now_componentSource = dateSource[component_Second];
                    let index = now_componentSource.firstIndex(of: String(nowomponents.second!)) ?? -1;
                    if index != -1 && now_componentSource.count > index{
                        row_Second = index;
                        datePickerMine.selectRow(row_Second, inComponent: component_Second, animated: true);
                    }
                }

            }
        }
    }
    //MARK: 配置文字字体和颜色
    ///文字大小
    public var textFont_Selected: UIFont? = nil {
        didSet{
            self.datePickerMine.reloadAllComponents()
        }
    }
    ///文字大小
    public var textFont_Normal: UIFont = UIFont.systemFont(ofSize: 18) {
        didSet{
            self.datePickerMine.reloadAllComponents()
        }
    }
    /// 文字颜色正常
    public var textColor_Normal: UIColor = UIColor.black{
        didSet{
            self.datePickerMine.reloadAllComponents()
        }
    }
    /// 文字颜色选中
    public var textColor_Selected: UIColor? = nil{
        didSet{
            self.datePickerMine.reloadAllComponents()
        }
    }
}

